﻿using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.LoginLoggerCommand;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.Management.SteamApplication.CommandHandler.LoginLoggerCommandHandler
{
    public class LoginLoggerCommandHandler : IRequestHandler<LoginLoggerCommand, APIRequst<APIPageing<List<LoginLogger>>>>
    {
        public LoginLoggerCommandHandler(IBaseRepository<LoginLogger> loginloggerRepository)
        {
            LoginloggerRepository = loginloggerRepository;
        }

        public IBaseRepository<LoginLogger> LoginloggerRepository { get; }

        public async Task<APIRequst<APIPageing<List<LoginLogger>>>> Handle(LoginLoggerCommand request, CancellationToken cancellationToken)
        {
            var path = LoginloggerRepository.GetAll();
            var TotalCount = path.Count();
            var pageCount = (int)Math.Ceiling(TotalCount * 1.0 / request.pageSize);
            var list = path.OrderBy(a => a.LoginId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return new APIRequst<APIPageing<List<LoginLogger>>>
            {
                Code = RequstCode.成功,
               Msg = "获取成功",
                Data = new APIPageing<List<LoginLogger>>
                { 
                 Total=pageCount,
                 TotalCount=TotalCount,
                 Data=list
                }
            };
        }
    }
}
